import {
    Dimensions, StatusBar, Text,
    View,
    TouchableOpacity,
    Modal
} from "react-native";
import React from "react";
import CS from "./CS";

const {width: sw, height: sh, scale: ss} = Dimensions.get('window');
module.exports = {
    headerPaddingTop: sw * 0.052,
    headerHeight: sw * 0.16666,
    activeOpacity: 0.5,
    _getStatusBar(isDark, hidden) {
        return <StatusBar backgroundColor={'rgba(255,255,255,0)'} translucent={true} animated={true}
                          hidden={hidden == true ? true : false}
                          barStyle={isDark ? 'dark-content' : 'light-content'}/>;
    },

    _renderEmptyView() {
        return (
            <View style={{
                justifyContent: 'center',
                alignItems: 'center',
                marginTop: sw * 0.5,
            }}>
                <Text style={{fontSize: 20, color: CS.gray33}}>老板,没有找到符合书籍 </Text>
                <Text style={{fontSize: 14, color: CS.gray66, marginTop: 10}}>清空搜索栏显示全部书籍 </Text>
            </View>
        )
    },

    _showDialog(self, title, content, _callback) {//
        if (self.state.isShowDialog === true) {
            return (
                <View
                    style={{
                        width: sw,
                        height: sh,
                        position: 'absolute',
                        backgroundColor: CS.tran,
                    }}
                >
                    <View style={{
                        flex: 1, backgroundColor: CS.gray66tran,
                        justifyContent: 'center',
                        alignItems: 'center',
                    }}>
                        <View style={{
                            width: sw * 0.6,
                            backgroundColor: CS.white,
                            borderRadius: sw * 0.02,
                            borderColor: CS.tran,
                            borderWidth: 1 / ss,
                            alignItems: 'center'
                        }}>
                            <Text
                                style={{fontSize: sw * 0.04, color: CS.greenDark, marginTop: sw * 0.02}}>{title}</Text>
                            <View
                                style={{
                                    backgroundColor: CS.green,
                                    width: sw * 0.6,
                                    height: 1 / ss,
                                    marginTop: sw * 0.02
                                }}/>
                            <Text style={{fontSize: sw * 0.035, color: CS.gray33, margin: sw * 0.05}}>{content}</Text>
                            <View
                                style={{
                                    backgroundColor: CS.green,
                                    width: sw * 0.6,
                                    height: 1 / ss,
                                    marginTop: sw * 0.02
                                }}/>
                            <View style={{flexDirection: 'row'}}>
                                <TouchableOpacity
                                    style={{
                                        justifyContent: 'center',
                                        alignItems: 'center',
                                        flex: 1,
                                    }}
                                    onPress={() => {
                                        _callback(false);
                                        self.setState({
                                            isShowDialog: false,
                                        });
                                    }}
                                >
                                    <Text style={{fontSize: sw * 0.04, color: CS.gray99}}>取消</Text>
                                </TouchableOpacity>
                                <View
                                    style={{
                                        backgroundColor: CS.green,
                                        width: 1 / ss,
                                        height: sw * 0.065,
                                        marginTop: sw * 0.02,
                                        marginBottom: sw * 0.02
                                    }}/>
                                <TouchableOpacity
                                    onPress={() => {
                                        _callback(true);
                                        self.setState({
                                            isShowDialog: false,
                                        });
                                    }}
                                    style={{
                                        justifyContent: 'center',
                                        alignItems: 'center',
                                        flex: 1,
                                    }}>
                                    <Text style={{fontSize: sw * 0.04, color: CS.gray33}}>确定</Text>
                                </TouchableOpacity>
                            </View>

                        </View>

                    </View>
                </View>
            )
        }
    },
    _getSize(size) {
        if (size == null) return '';
        if (size < 1024) {
            return this._formatNumber(size) + "B";
        } else if (size < 1048576) {
            return this._formatNumber(size / 1024) + "KB";
        } else if (size < 1073741824) {
            return this._formatNumber(size / 1024000) + "MB";
        } else {
            return this._formatNumber(size / 1024000111) + "GB";
        }
    },

    _formatNumber(size) {
        let str = size.toString();
        let p = str.indexOf('.');
        if (p != -1) {
            return str.substr(0, p + 3);
        } else {
            return str;
        }
    },

    _isContainChinese(str) {
        if (escape(str).indexOf("%u") < 0) {
            return false;
        } else {
            return true;
        }
    }
};